package com.zjuee.web.company.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zjuee.base.bean.response.R;
import com.zjuee.base.constant.Common;
import com.zjuee.base.dto.common.company.CompanyDto;
import com.zjuee.base.valid.Update;
import com.zjuee.service.company.service.CompanyService;
import com.zjuee.service.company.vo.CompanyRspVo;
import com.zjuee.service.company.vo.CompanyVo;
import com.zjuee.service.company.vo.LoginRespVo;
import com.zjuee.base.model.common.company.Company;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;

@Slf4j
@RestController
@RequestMapping(Common.Service.ZJUEE_CO_AUTH_COMPANY_URL_PREFIX)
public class CompanyController{

    @Resource
    private CompanyService companyService;

    /**
     * 非运输企业新增
     *
     * @param companyInfo CompanyVo
     * @return Long
     */
    @PostMapping("/add")
    public R<Long> addCompany(@Validated(Integer.class) @RequestBody CompanyVo companyInfo) {
        try {
            Long add = companyService.add(companyInfo);
            if (null != add) {
                return R.ok(add);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }
    /**
     * 非运输企业修改
     */
    @PostMapping("/update")
    public R<Boolean> update(@Validated(Update.class) @RequestBody CompanyVo user) {
        try {
            Boolean update = companyService.update(user);
            if (update) {
                return R.ok(update);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail();
    }

    /**
     * 查询详情 - 企业整体信息
     *
     * @param id Long
     * @return CompanyVo
     */
    @GetMapping("/info/{id}")
    public R<CompanyVo> selectById(@NotNull @PathVariable(value = "id") Long id) {
        try {
//            LoginDto loginDto = (LoginDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            CompanyVo select = companyService.selectById(id, 0L);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }


    /**
     * 查询基本信息
     *
     * @param id Long
     * @return Company
     */
    @GetMapping("/info/base/{id}")
    public R<Company> selectBaseById(@NotNull @PathVariable(value = "id") Long id) {
        try {
//            LoginDto loginDto = (LoginDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            Company select = companyService.selectBaseById(id,0L);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }
    /**
     * 查询部分信息 - 登录处理
     *
     * @param id Long
     * @return CompanyRspVo
     */
    @GetMapping("/part/info/{id}")
    public R<CompanyRspVo> selectPartInfoById(@NotNull @PathVariable(value = "id")Long id) {
        try {
//            LoginDto loginDto = (LoginDto) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            CompanyRspVo select = companyService.selectPartInfoById(id);
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }
    /**
     * 查询基本信息 - 分页
     *
     * @param companyDto CompanyDto
     * @return Company
     */
    @PostMapping("/list")
    public R<Page<Company>> listCompany(@RequestBody CompanyDto companyDto) {
        try {
            Page<Company> page = companyService.list(companyDto);
            if (null != page) {
                return R.ok(page);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }
    /**
     * 获取登录信息
     *
     * @return R<LoginVo>
     */
    public R<LoginRespVo> getLoginInfo() {
        try {
            LoginRespVo select = companyService.selectLoginInfo();
            if (null != select) {
                return R.ok(select);
            }
        } catch (Exception e) {
            return R.fail(e.getMessage());
        }
        return R.fail("Resource does not exist");
    }
}
